package com.example.weatherserctary2.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class HttpGet {
	private static final String DATA_URL = "http://apis.haoservice.com/weather";
	private static final String DATA_KEY = "8a2a8c5b04cd413499ec89b9d6642b86";
	private static final String DATA_FORMAT = "2";
	
	public static Weather getWeatherData(String cityname){
		String url_str = DATA_URL+"?cityname="+encodeStr(cityname)
				+"&key="+DATA_KEY;
		Weather wt = null;  //声明
		try {
			URL url = new URL(url_str);
			URLConnection urlConn = url.openConnection();
			urlConn.connect();
			InputStream is = urlConn.getInputStream();
			BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
			String line = "";
			StringBuffer sbf = new StringBuffer();
			while((line=bfr.readLine())!=null){
				sbf.append(line);
			}
			System.out.println("........sbf.....");
			System.out.println(sbf.toString());
			bfr.close();
			is.close();
			wt = new Weather();  //实例化
			JSONObject jsonObject = new JSONObject(sbf.toString());
			JSONObject mainObject = jsonObject.getJSONObject("result");
			JSONObject skObject = mainObject.getJSONObject("sk");
			JSONObject tdObject = mainObject.getJSONObject("today");
			wt.setTemp(skObject.getString("temp"));
			wt.setWind_direction(skObject.getString("wind_direction"));
			wt.setWind_strength(skObject.getString("wind_strength"));
			wt.setHumidity(skObject.getString("humidity"));
			wt.setTime(skObject.getString("time"));
			
			wt.setCity(tdObject.getString("city"));
			wt.setDate_y(tdObject.getString("date_y"));
			wt.setWeek(tdObject.getString("week"));
			wt.setWeather(tdObject.getString("weather"));
			
			JSONArray futureArray = mainObject.getJSONArray("future");
			Future[] ft = new Future[futureArray.length()];
			for(int i = 0;i<futureArray.length();i++){
				JSONObject futureObject = futureArray.getJSONObject(i);
				ft[i] = new Future();
				ft[i].setTemperature(futureObject.getString("temperature"));
				ft[i].setWeather(futureObject.getString("weather"));
				ft[i].setWeek(futureObject.getString("week"));
				ft[i].setDate(futureObject.getString("date"));
			}
			System.out.println("....ft.......");
			System.out.println(ft);
			wt.setFuture(ft);
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return wt;
		
	}
	
	//对汉字编码
	public static String encodeStr(String cityname){
		String str = "";
		try {
			str = URLEncoder.encode(cityname, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
}
